package org.gpp.proj1.camera;

import java.util.LinkedList;
import java.util.List;

import org.easymock.EasyMock;
import org.gpp.proj1.camera.action.CameraAction;
import org.gpp.proj1.util.test.JMETestCase;

import com.jme.renderer.Camera;
import com.jme.scene.CameraNode;
import com.jme.scene.Node;
import com.jme.system.DisplaySystem;

public class CameraDirectorTest extends JMETestCase {

	/*
	 * Test method for 'org.gpp.proj1.camera.CameraDirector.CameraDirector(CameraNode)'
	 */
	public void testCameraDirector() {
		try {
			new CameraDirector( null );
			fail();
		} catch( AssertionError e ) {
			// expected error
		}
		
		Camera cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();		
		CameraNode node = new CameraNode( "CamNode", cam );
		
		try {
			new CameraDirector( node );
		} catch( AssertionError e ) {
			fail();
		}
	}

	/*
	 * Test method for 'org.gpp.proj1.camera.CameraDirector.updateCamera(float)'
	 */
	public void testUpdateCamera() {
		Camera cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();		
		CameraNode node = new CameraNode( "CamNode", cam );
		
		List<CameraAction> actions = new LinkedList<CameraAction>();
		CameraAction mockAction = EasyMock.createMock( CameraAction.class );
		
		actions.add( mockAction );
		
		CameraDirector director = new CameraDirector( node, actions );
		
		EasyMock.reset( mockAction );
		mockAction.isFinished(); EasyMock.expectLastCall().andReturn( false ).times( 1 );
		mockAction.updateAction( 0.0f, node ); EasyMock.expectLastCall().once();
		mockAction.isFinished(); EasyMock.expectLastCall().andReturn( true ).times( 1 );
		EasyMock.replay( mockAction );
		
		// One iteration to update the action
		director.updateCamera( 0.0f );
		
		// The second iteration must remove the current action
		director.updateCamera( 0.0f );
		
		EasyMock.verify( mockAction );
		
		assertNull( director.currentAction );
	}

	/*
	 * Test method for 'org.gpp.proj1.camera.CameraDirector.queueAction(CameraAction)'
	 */
	public void testQueueAction() {
		Camera cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();		
		CameraNode node = new CameraNode( "CamNode", cam );
		
		List<CameraAction> actions = (List<CameraAction>) EasyMock.createMock( List.class ) ;
		CameraAction mockAction = EasyMock.createMock( CameraAction.class );
		
		CameraDirector director = new CameraDirector( node, actions );

		EasyMock.reset( actions );
		actions.add( mockAction ); EasyMock.expectLastCall().andReturn(true).once();
		EasyMock.replay( actions );
		
		director.queueAction( mockAction );
		
		EasyMock.verify( actions );
	}

	/*
	 * Test method for 'org.gpp.proj1.camera.CameraDirector.setFocusedObject(Node)'
	 */
	public void testSetFocusedObject() {
		Camera cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();		
		CameraNode node = new CameraNode( "CamNode", cam );
		
		CameraDirector director = new CameraDirector( node );
		
		Node focusedNode = new Node( "focusedNode" );
		
		assertNull( director.focusedObject ); 
		
		director.setFocusedObject( focusedNode );
		
		assertEquals( focusedNode, director.focusedObject );
	}

	/*
	 * Test method for 'org.gpp.proj1.camera.CameraDirector.getCamera()'
	 */
	public void testGetCamera() {
		Camera cam = DisplaySystem.getDisplaySystem().getRenderer().getCamera();		
		CameraNode node = new CameraNode( "CamNode", cam );
		
		CameraDirector director = new CameraDirector( node );
		
		assertEquals( node, director.getCamera() );
	}

}
